package com.lwww.infinitechat.authenticationservice.exception;

import com.lwww.infinitechat.authenticationservice.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    /**
     * 用户异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = UserException.class)
    public Result<?> handleUserException(UserException e){
        log.error("用户注册异常：{}",e.getMessage());
        return Result.userError(e.getCode(),e.getMessage());
    }

    /**
     * 数据库异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = DataBaseException.class)
    public Result<?> handleDataBaseException(DataBaseException e){
        log.error("数据库异常：{}",e.getMessage());
        return Result.databaseError(e.getMessage());
    }

    /**
     * 验证码异常
     * @param e
     * @return
     */
    public Result<?> handleCodeException(CodeException e){
        log.error("验证码异常：{}",e.getMessage());
        return Result.userError(e.getCode(),e.getMessage());
    }

    /**
     * 未知异常
     * @param e
     * @return
     */
    @ExceptionHandler(value = Throwable.class)
    public Result<?> handleGlobalException(Throwable e){
        log.error("未知错误：{}",e.getMessage());
        return Result.serverError(e.getMessage());
    }
}
